// personDetail.js
var ajax = require("../../utils/network.js");
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  },
  onShow: function () {
    this.userInfo();
  },


  //用户信息
  userInfo: function () {
    var that = this;
    var token = wx.getStorageSync("token");
    var URL = ajax.server() + 'member/info-api?token=' + token;
    var Params = {};
    var Method = 'GET';
    var Message = '';
    ajax.requestLoading(URL, Params, Method, Message, function (res) {
      console.log('用户信息---', res)
      if (res.code == 0) {
        var member_name = res.data.member_name;
        var mobile = res.data.mobile;
        var sex = res.data.sex
        if (sex == 0) {
          var gener = '保密'
        }
        if (sex == 1) {
          var gener = '男'
        }
        if (sex == 2) {
          var gener = '女'
        }
        that.setData({
          member_name: member_name,
          mobile: mobile,
          gener: gener
        })
      }
    }, function () { })
  },

  //修改用户名
  userName: function () {
    wx.navigateTo({
      url: '../userName/userName',
    })
  },

  //手机
  mobile: function () {
    wx.showToast({
      title: '暂未开启',
    })
  },

  //选择性别
  gender: function () {
    wx.showActionSheet({
      itemList: ['保密', '男', '女'],
      success: function (res) {
        console.log(res.tapIndex)
        var that = this;
        var token = wx.getStorageSync("token");
        var URL = ajax.server() + 'member/update-info?token=' + token
        var Params = { sex: res.tapIndex };
        var Method = 'POST';
        var Message = '';
        ajax.requestPost(URL, Params, Method, Message, function (res) {
          console.log('保存性别---', res)
          if (res.status == true) {
            wx.showToast({
              title: res.msg,
            })
          }
        }, function () {})
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  //退出登陆
  loginOut: function () {
    let token = wx.getStorageSync("token")
    if (token) {
      let token = wx.removeStorageSync('token');
      let mobile = wx.removeStorageSync("mobile");
      wx.navigateBack()
      wx.showToast({
        title: '退出成功',
      })
    }
  }
})